package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduPlanItemMapper;
import com.nbjtjc.safe.model.edu.EduPlanItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EduPlanItemService {
	@Autowired
	EduPlanItemMapper eduPlanItemMapper;
	
	  public Result add(EduPlanItem eduPlanItem){
	        Result result=new Result();
	        if(ObjectUtil.isNull(eduPlanItem)
	        		||ObjectUtil.isNull(eduPlanItem.getPlanId())
	        		||ObjectUtil.isNull(eduPlanItem.getPlanItems())
	        		){
	            result.setFalse("培训计划ID和培训内容不能为空");
	            return result;
	        }

	        if(ObjectUtil.isNull(eduPlanItem.getId())
	        		|| ObjectUtil.isNull(eduPlanItemMapper.selectByPrimaryKey(eduPlanItem.getId()))){
	        	
	        	int res=eduPlanItemMapper.insertSelective(eduPlanItem);
	            
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;

	        }
	        }else
	        {
	        	int res=eduPlanItemMapper.updateByPrimaryKeySelective(eduPlanItem);
	        	 if(res==0){
	        		 result.setFalse("更新失败");
			         return result;

	        	 }
	        }
	        result.setData(eduPlanItem);
	        return result;
	    }



	public Result delete(int eduPlanItemID){
	    Result result=new Result();
	    if(ObjectUtil.isNull(eduPlanItemID)){
	        result.setFalse("删除对象不能为空");
	        return result;
	    }
	    int res=eduPlanItemMapper.deleteByPrimaryKey(eduPlanItemID);
	    if(res==0){
	        result.setFalse("删除失败");
	        return result;
	        }
	    return result;
	}
	
	//根据条件查找调查项目
	
	public Result find(EduPlanItem eduPlanItem) {
	    Result result = new Result();
	    PageHelper.startPage(eduPlanItem.getPageCurrent(), eduPlanItem.getPageSize());
	    
	    List<EduPlanItem> eduPlanItems = eduPlanItemMapper.select(eduPlanItem);
	    result.setData(new PageInfo<>(eduPlanItems));
	
	    return result;
	}
	
	

}
